package com.huawei123.hasee.cniao5store.utils;

import android.util.Log;
import android.widget.Toast;

import com.huawei123.hasee.cniao5store.base.StoreApplication;

/**
 * Log帮助类
 * Created by hasee on 2016/2/9.
 */
public class LogUtils {
    public static final boolean DEBUG = true;
    public static final String TAG = ">>>>>>>>";
    public static final String ERRMES = "luohao";
    public static final String APPTAG = "luohao123";

    static String clazz = Thread.currentThread().getStackTrace()[3].getClassName();//所在类名
    static String method = Thread.currentThread().getStackTrace()[3].getMethodName();//所在方法名

    /**
     * 所在类
     *
     * @return
     */
    public static String clazz() {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        if (trace == null || trace.length == 0) {
            return "???????";
        }
        return trace[4].getClassName();
    }

    /**
     * 所在方法
     *
     * @return
     */
    public static String getMethodName() {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        if (trace == null || trace.length == 0) {
            return "??????";
        }
        return trace[4].getMethodName();
    }

    /**
     * 所在行号
     *
     * @return
     */
    public static int line() {
        StackTraceElement[] trace = Thread.currentThread().getStackTrace();
        if (trace == null || trace.length == 0) {
            return -1;
        }
        return trace[4].getLineNumber();
    }

    public static void debug(String tag, String msg) {
        if (DEBUG) {
            Log.d(tag, "类名< " + clazz() + ".class >方法名：" + getMethodName() + "()" + "---第" + line() + "行>>>>>>" + msg
                    + "<<<<<<");
        }
    }

    public static void debug(String msg) {
        if (DEBUG) {
            Log.d(APPTAG, "类名< " + clazz() + ".class >方法名：" + getMethodName() + "()" + "---第" + line() + "行>>>>>>" +
                    msg + "<<<<<<");
        }
    }

    public static void bugMes(String msg) {
        if (DEBUG) {
            Log.w(ERRMES, "类名< " + clazz() + ".class >方法名：" + getMethodName() + "()" + "---第" + line() + "行>>>>>>" +
                    msg + "<<<<<<");
        }
    }

    public static void bugErr(String msg) {
        if (DEBUG) {
            System.err.println(TAG + "类名< " + clazz() + ".class >方法名：" + getMethodName() + "()" + "---第" + line() +
                    "行>>>>>>" + msg + "<<<<<<");
        }
    }


    public static void errMes(String msg, Throwable tr) {
        if (DEBUG) {
            Log.e(ERRMES, "类名< " + clazz() + ".class >方法名：" + getMethodName() + "()" + "---第" + line() + "行>>>>>>" +
                    Log.getStackTraceString(tr) + "<<<<<<");
        }
    }

    public static void showInfo(String msg) {
        if (DEBUG) {
            Log.i(ERRMES, "类名< " + clazz() + ".class >方法名：" + getMethodName() + "()" + "---第" + line() + "行>>>>>>" +
                    msg + "<<<<<<");
        }
    }

    public static void showToastLong(String msg) {
        Toast.makeText(StoreApplication.getContext(), msg, Toast.LENGTH_LONG).show();
    }

    public static void showToastShort(String msg) {
        Toast.makeText(StoreApplication.getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 之前显示的内容
     */
    private static String oldMsg;
    /**
     * Toast对象
     */
    private static Toast toast = null;
    /**
     * 第一次时间
     */
    private static long oneTime = 0;
    /**
     * 第二次时间
     */
    private static long twoTime = 0;

    /**
     * 显示长拼装Toast
     *
     * @param message
     */
    public static void showToast(String message) {
        if (toast == null) {
            toast = Toast.makeText(StoreApplication.getContext(), message, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (message.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = message;
                toast.setText(message);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

}
